package com.SDU.policedispatch.service.impl.user;


import com.SDU.policedispatch.entity.Department;
import com.SDU.policedispatch.entity.PersonnelInfo;
import com.SDU.policedispatch.entity.Schedules;
import com.SDU.policedispatch.entity.User;
import com.SDU.policedispatch.mapper.DepartmentMapper;
import com.SDU.policedispatch.mapper.PersonnelInfoMapper;
import com.SDU.policedispatch.mapper.ScheduleMapper;
import com.SDU.policedispatch.mapper.UserMapper;
import com.SDU.policedispatch.service.user.UserAccountService;
import com.SDU.policedispatch.service.utils.UserDetailsImpl;
import com.SDU.policedispatch.utils.JwtUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
public class UserAccountServiceImpl implements UserAccountService {
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private ScheduleMapper scheduleMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private PersonnelInfoMapper personnelInfoMapper;

    @Override
    public Map<String, String> UserRegister(Integer departmentId, String username, String password, Integer type) {
        Map<String, String> map = new HashMap<>();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("department_id", username);
        User user = userMapper.selectOne(queryWrapper);
        if(user != null) {
            map.put("error_message", "部门ID已存在");
            return map;
        }
        String encodedPassword = passwordEncoder.encode(password);
        User NewUser = new User(departmentId, username, encodedPassword, type);
        userMapper.insert(NewUser);
        map.put("error_message", "success");
        return map;
    }

    @Override
    public Map<String, String> UserLogin(String username, String password) {
        UsernamePasswordAuthenticationToken authenticationToken =
                new UsernamePasswordAuthenticationToken(username, password);

        System.out.println(username + " " + password);

        Authentication authenticate = authenticationManager.authenticate(authenticationToken);  // 登录失败，会自动处理
        UserDetailsImpl loginUser = (UserDetailsImpl) authenticate.getPrincipal();
        User user = loginUser.getUser();
        String jwt = JwtUtil.createJWT(user.getDeptId().toString());

        Map<String, String> map = new HashMap<>();
        map.put("error_message", "success");
        map.put("token", jwt);

        return map;
    }

    @Override
    public Map<String, String> UserInfo() {
        UsernamePasswordAuthenticationToken authenticationToken =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
        User user = loginUser.getUser();

        Map<String, String> map = new HashMap<>();
        Department d = departmentMapper.selectById(user.getDeptId());
        map.put("department", d.getDeptName());
        map.put("username", user.getUsername());
        return map;
    }

    @Override
    public Map<String, String> UserAccount(String id) {
        UsernamePasswordAuthenticationToken authenticationToken =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
        User user = loginUser.getUser();

        Map<String, String> map = new HashMap<>();
        PersonnelInfo p = personnelInfoMapper.selectPersonnelInfoById(id);
        if(p == null) {
            return null;
        }
        map.put("姓名", p.getName());
        map.put("警号", id);
        map.put("电话", p.getPhoneNumber());
        map.put("性别", p.getSex());
        return map;
    }
}
